from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.permissions import IsAdminUser
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response

from goods.models import SKUImage, SKU
from meiduo_admin.serializers.images import ImagesSerializer, SKUSerializer
from meiduo_admin.utils import PageNum


class ImagesView(ModelViewSet):
    serializer_class = ImagesSerializer
    queryset = SKUImage.objects.all()
    permission_classes = [IsAdminUser]
    pagination_class = PageNum

    def simple(self,request):
        skus = SKU.objects.all()
        ser = SKUSerializer(skus,many=True)

        return Response(ser.data)

    # def create(self, request, *args, **kwargs):
    #     """上传图片到fast_DFS"""
    #     # 获取数据
    #     data = request.data
    #     # 验证数据
    #     ser = self.get_serializer(data=data)
    #     ser.is_valid()
    #
    #     # # 3 建立fdfs客户端
    #     # client = Fdfs_client(settings.FASTDFS_PATH)
    #     # file = request.FILES.get('image')
    #     # # 4 上传图片
    #     # res = client.upload_by_buffer(file.read())
    #     # # 判断是否上传成功
    #     # if res['Status'] != 'Upload successed.':
    #     #     return Response({'error': '图片上传失败'})
    #     #
    #     # # 保存图片表
    #     # img = SKUImage.objects.create(sku=ser.validated_data['sku'], image=res['Remote file_id'])
    #     ser.save()
    #     # 返回保存后的数据
    #     return Response(ser.data, status=201)